<?php

class SessionDB extends Model {

    public $session_id;
    public $ip_address;
    public $last_activity;
    public $login_times;
    public $user_data;

    public function __construct() {
        parent::__construct();
        //$this->_initialize($params);
    }

    public function start($ip) {
        $sql = sprintf("INSERT INTO `l5_sessions`(`ip_address`,`last_activity`, `login_times`)
				VALUES('%s'," . time() .", %s)", $ip, 1);

        $this->db->queryshort($sql, DB_NAME);
        $row_affected = $this->db->affectedRow();

        if ($row_affected > 0) {
            //return 'Registration successfully';
            return true;
        } else {
            //return 'Registration unsuccessfully';
            return false;
        }
    }

    public function destroy($ip) {
        if (empty($ip)) {
            return false;
        }

        $sql = "DELETE FROM `l5_sessions` WHERE `ip_address`='%s'";
        $sql = sprintf($sql, $ip);
        $this->db->queryshort($sql, DB_NAME);
        $affectedRow = $this->db->affectedRow();

        if ($affectedRow > 0) {
            return true;
        } else {
            return false;
        }

        return false;
    }

    public function block($ip) {
        $sql = "UPDATE l5_sessions SET blocked=1, last_activity=" .time(). " WHERE ip_address='%s'";
        $sql = sprintf($sql, $ip);

        $this->db->queryshort($sql, DB_NAME);
        $affectedRow = $this->db->affectedRow();

        if ($affectedRow > 0) {
            //return 'User updated successfully.';
            return true;
        } else {
            return false;
            //return 'User updated successfully.';
        }

        return false;
    }

    public function update($ip) {
        $sql = "UPDATE l5_sessions SET last_activity=" .time(). ", login_times=login_times+1 WHERE ip_address='%s'";
        $sql = sprintf($sql, $ip);

        $this->db->queryshort($sql, DB_NAME);
        $affectedRow = $this->db->affectedRow();

        if ($affectedRow > 0) {
            //return 'User updated successfully.';
            return true;
        } else {
            return false;
            //return 'User updated successfully.';
        }

        return false;
    }

    public function get($ip) {
        if (trim($ip) == '') {
            return FALSE;
        }

        $sql = sprintf("SELECT * FROM `l5_sessions` WHERE `ip_address`='%s'", $ip);

        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            while ($row = mysql_fetch_object($this->db->result)) {
                return $row;
            }
            return TRUE;
        }
        return NULL;
    }

    private function _isExist($ip) {
        if (trim($ip) == '') {
            return FALSE;
        }

        $sql = sprintf("SELECT * FROM `l5_sessions` WHERE `ip_address`='%s'", $ip);
        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            while ($row = mysql_fetch_object($this->db->result)) {
                return $row;
            }
            return TRUE;
        }
        return NULL;
    }
}
?>

